<?php

namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;

class storeapplyModel extends Model
{
    protected $table = 'store_apply';
    protected $primaryKey = 'id';
    protected $appends = ['status_text', 'images_arr'];

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * @return string
     */
    public function getStatusTextAttribute()
    {
        if($this->attributes['status'] === -1){
            $status_text = '已拒绝';
        }elseif ($this->attributes['status'] === 1){
            $status_text = '已通过';
        }else{
            $status_text = '未审核';
        }
        return $status_text;
    }

    /**
     * @return array
     */
    public function getImagesArrAttribute()
    {
        $images_arr = [];
        if(!empty($this->attributes['images'])){
            $images_arr = explode(';', $this->attributes['images']);
        }
        return $images_arr;
    }

    /**
     * @return array
     */
    public function getOpenweeksTextArrAttribute($weeks = '')
    {
        $openweeks_text_arr = [];
        if(empty($weeks)){
            $weeks = $this->attributes['openweeks'];
        }
        if(!empty($weeks)){
            $weeks_arr = (new \storeModel())->getServiceAreaIdsArrAttribute($weeks);
            foreach ($weeks_arr as $week){
                switch ($week){
                    case '1':
                        $openweeks_text_arr[] = '星期一';
                        break;
                    case '2':
                        $openweeks_text_arr[] = '星期二';
                        break;
                    case '3':
                        $openweeks_text_arr[] = '星期三';
                        break;
                    case '4':
                        $openweeks_text_arr[] = '星期四';
                        break;
                    case '5':
                        $openweeks_text_arr[] = '星期五';
                        break;
                    case '6':
                        $openweeks_text_arr[] = '星期六';
                        break;
                    case '7':
                        $openweeks_text_arr[] = '星期日';
                        break;
                }
            }
            unset($week);
            unset($weeks_arr);
        }
        unset($weeks);
        return $openweeks_text_arr;
    }

    /**
     * @return string
     */
    public function getOpenweeksTextAttribute($weeks = '')
    {
        $openweeks_text = '';
        if(empty($weeks)){
            $weeks = $this->attributes['openweeks'];
        }
        if(!empty($weeks)){
            $weeks_arr = $this->getOpenweeksTextArrAttribute($weeks);
            $openweeks_text = implode('；', $weeks_arr);
            unset($weeks_arr);
        }
        unset($weeks);
        return $openweeks_text;
    }

    /**
     * @return string
     */
    public function getOpenhoursTextAttribute($hours = '')
    {
        $openhours_text = '';
        if(empty($hours)){
            $hours = $this->attributes['openhours'];
        }
        if(!empty($hours)){
            if(mb_strpos($hours, '-') !== false){
                $openhours_text = mb_ereg_replace('-', '至', $hours);
            }else{
                $openhours_text = $hours;
            }
        }
        unset($weeks);
        return $openhours_text;
    }



    public function member()
    {
        return $this->hasOne(\memberModel::class, 'id', 'member_id')->select(['id', 'phone', 'nickname', 'realname', 'avatar']);
    }

}